require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UICharactorInfoHeroSkillSelect = {};
HelperFunc.SetUIEnv(UICharactorInfoHeroSkillSelect)
local this = UICharactorInfoHeroSkillSelect;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.initSkillInfoConfig = nil;
this.curSkillInfoConfig = nil;
this.curHeroInfo = nil
this.skillFieldType = nil;

this.hasInit = false;

this.btnBack = nil;
this.btnConfirm = nil;
this.txtConfirm = nil;
---@type FairyGUI.GList
this.objGrid = nil;
this.tableItems = {};
this.selectIndex = 0

function UICharactorInfoHeroSkillSelect.Init(skillInfo,type)--todo 应该小写 暂时先大写
    initSkillInfoConfig = skillInfo;
    curHeroInfo = nil;
    skillFieldType = type;
    local name,package = getUIName(PanelNames.UICharactorInfoHeroSkillSelect);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end
---@param obj FairyGUI.GComponent
function UICharactorInfoHeroSkillSelect.onCreate(obj)
    --gameObject = obj;
    --transform = obj.transform;

    if not hasInit then
        hasInit = true;
        btnBack = obj:GetChild("BackBtn")
        btnConfirm = obj:GetChild("ConfirmBtn")
        txtConfirm = btnConfirm:GetChild("title")
        objGrid = obj:GetChild("Scroll View")
        objGrid.itemRenderer = this.ListItemRender
        this.addEvent();
    end

    txtConfirm.text = getLanguage("Btn_Confirm");

    this.refresh();
end

function UICharactorInfoHeroSkillSelect.addEvent()
    btnBack.onClick:Add(this.onClickClose)
    btnConfirm.onClick:Add(this.onClickConfirm)
end

function UICharactorInfoHeroSkillSelect.removeEvent()
    btnBack.onClick:Clear()
    btnConfirm.onClick:Clear()
end

function UICharactorInfoHeroSkillSelect.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Skill,this.onClickClose)
end

function UICharactorInfoHeroSkillSelect.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Skill,this.onClickClose)
end

function UICharactorInfoHeroSkillSelect.OnDestroy()
    this.removeEvent();
end

function UICharactorInfoHeroSkillSelect.OnEnable()
    this.LocalListenEvent();
end

function UICharactorInfoHeroSkillSelect.OnDisable()
    this.LocalCloseEvent();
end
function UICharactorInfoHeroSkillSelect.ListItemRender(index,go)
    index = index + 1
    local data = curSkillInfoConfig[index]
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.UICharactorInfoHeroSkillSelectItem);
    local item = {}
    item["obj"] = go
    item["skill"] = data.skill;
    item["hero"] = data.hero;
    ctrlItem.initData(item["obj"],item,this.onClickSelect,this.onClickItemIcon);

    ctrlItem.select(item["obj"],item["skill"] == curSkillInfoConfig);
end

function UICharactorInfoHeroSkillSelect.refresh()
    local imitationSkillInfos = logicMgr.SkillManager.getImitationSkillInfos(skillFieldType);
    curSkillInfoConfig = imitationSkillInfos;
    objGrid.numItems = #imitationSkillInfos
end


function UICharactorInfoHeroSkillSelect.onClickClose()
    closeUI(PanelNames.UICharactorInfoHeroSkillSelect);
end


function UICharactorInfoHeroSkillSelect.onClickConfirm()
    if curSkillInfoConfig == nil or curSkillInfoConfig == initSkillInfoConfig then
        return;
    end

    local selfInfo = logicMgr.HeroManager.getMyself();
    local skill = logicMgr.SkillManager.getRealSkillByStar(curSkillInfoConfig,selfInfo.level);
    if skill==nil then
        UISysTips.AddMsg(getLanguage("Sys_MsgSkillNotExist"));
        return;
    end
    skillHandler.sendImitationSkill(curHeroInfo.partnerId,skill.f_SkillID);
end


function UICharactorInfoHeroSkillSelect.onClickSelect(go,data)
    curSkillInfoConfig = data["skill"];
    curHeroInfo = data["hero"];
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.UICharactorInfoHeroSkillSelectItem);
    for i = 1, #tableItems do
        ctrlItem.select(tableItems[i]["obj"],tableItems[i]["skill"] == curSkillInfoConfig);
    end
end

function UICharactorInfoHeroSkillSelect.onClickItemIcon(go,data)
    local currentHeroData = logicMgr.HeroManager.getMyself();
    local level = logicMgr.SkillManager.getSkillLv(data["skill"],currentHeroData.star);
    showUI(PanelNames.UICharactorInfoHeroSkillDetails,data["skill"],level);
end



return UICharactorInfoHeroSkillSelect;
